package bt.sim.student;

import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
/**
 * Student model.

将表结构放在此，消除记忆负担
mysql> desc t_student;
+----------+---------------+------+-----+---------+----------------+
| Field    | Type          | Null | Key | Default | Extra          |
+----------+---------------+------+-----+---------+----------------+
| stuId    | int(11)       | NO   | PRI | NULL    | auto_increment |
| stuNo    | varchar(20)   | YES  |     | NULL    |                |
| stuName  | varchar(10)   | YES  |     | NULL    |                |
| sex      | varchar(5)    | YES  |     | NULL    |                |
| birthday | date          | YES  |     | NULL    |                |
| gradeId  | int(11)       | YES  | MUL | NULL    |                |
| email    | varchar(20)   | YES  |     | NULL    |                |
| stuDesc  | varchar(1000) | YES  |     | NULL    |                |
+----------+---------------+------+-----+---------+----------------+

数据库字段名建议使用驼峰命名规则，便于与 java 代码保持一致，如字段名： userId
 */
public class Student extends Model<Student> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final Student me = new Student();
	
	/**
	 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
	 */
	public Page<Student> paginate(Map<String, String[]> conditions) {
		int pageNumber = Integer.parseInt(conditions.get("page")[0]);//第几页
		int pageSize = Integer.parseInt(conditions.get("rows")[0]); //每页几条
		
		StringBuilder sb = new StringBuilder("from t_student s, t_grade g where s.gradeId=g.id");
		
		if(StrKit.notBlank(conditions.get("stuNo"))) {
			sb.append(" and s.stuNo like '%" + conditions.get("stuNo")[0] +"%'");
		}
		if(StrKit.notBlank(conditions.get("stuName"))) {
			sb.append(" and s.stuName like '%" + conditions.get("stuName")[0] + "%'");
		}
		if(StrKit.notBlank(conditions.get("sex"))) {
			sb.append(" and s.sex = '" + conditions.get("sex")[0] + "'");
		}
		if(StrKit.notBlank(conditions.get("bbirthday"))) {
			sb.append(" and TO_DAYS(s.birthday) >=  TO_DAYS('" + conditions.get("bbirthday")[0] + "')");
		}
		if(StrKit.notBlank(conditions.get("ebirthday"))) {
			sb.append(" and TO_DAYS(s.birthday) <=  TO_DAYS('" + conditions.get("ebirthday")[0] + "')");
		}
		if(StrKit.notBlank(conditions.get("gradeId"))) {
			sb.append(" and s.gradeId = " + Integer.parseInt(conditions.get("gradeId")[0]) + "");
		}
		return paginate(pageNumber, pageSize, "select *", sb.toString());
	}
	public int studentRemove(String delIds) {
		String sql = "delete from t_student where stuId in (" + delIds + ")";
		return Db.update(sql);
	}
}
